package top.chmin.project.cloud.code;


import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.chmin.project.cloud.build.BuildData;
import top.chmin.project.cloud.build.Builder;
import top.chmin.project.cloud.sys.column.info.ColumnInfo;

import java.util.List;

/**
 * chmin
 * 2024/1/10 16:08
 * cloud-builder
 */
public interface BuilderAppender2 {

    Logger log = LoggerFactory.getLogger(BuilderAppender2.class);

    static String getFieldName(ColumnInfo columnInfo) {
        return StrUtil.toCamelCase(columnInfo.getColumnName());
    }

    static String getMethodName(ColumnInfo columnInfo) {
        return StringUtils.capitalize(getFieldName(columnInfo));
    }

    static String getSimpleType(ColumnInfo columnInfo) {
        return switch (columnInfo.getColumnType()) {
            case "varchar", "char" -> "String";
            case "int" -> "Integer";
            case "bigint" -> "Long";
            case "decimal" -> "BigDecimal";
            case "datetime" -> "LocalDateTime";
            default -> throw new RuntimeException("不支持的类型");
        };
    }

    static String getFullType(ColumnInfo columnInfo) {
        return switch (columnInfo.getColumnType()) {
            case "varchar", "char" -> "java.lang.String";
            case "int" -> "java.lang.Integer";
            case "bigint" -> "java.lang.Long";
            case "decimal" -> "java.math.BigDecimal";
            case "datetime" -> "java.time.LocalDateTime";
            default -> throw new RuntimeException("不支持的类型");
        };
    }

    List<String> append(BuildData2 buildData);

    default String fix(BuildData data, ColumnInfo columnInfo, String source) {
        source = Builder.fix(data, source);
        return source
                .replace("#c.columnCode", columnInfo.getColumnCode())
                .replace("#c.columnName", columnInfo.getColumnName())
                .replace("#c.columnComment", columnInfo.getColumnComment())
                .replace("#c.columnType", columnInfo.getColumnType())
                .replace("#c.columnLength", columnInfo.getColumnLength() + "")
                .replace("#c.columnPrecision", columnInfo.getColumnPrecision() + "")
                .replace("#c.columnScale", columnInfo.getColumnScale() + "")
                .replace("#c.columnNullable", ("N".equals(columnInfo.getColumnNullable()) ? "NOT " : "") + "NULL")
                .replace("#c.fieldName", getFieldName(columnInfo))
                .replace("#c.methodName", getMethodName(columnInfo))
                .replace("#c.simpleType", getSimpleType(columnInfo))
                .replace("#c.fullType", getFullType(columnInfo));
    }
}
